package com.wang.controller;

import com.wang.entity.TeacherWork;
import com.wang.entity.User;
import com.wang.entity.Work;
import com.wang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
  @RequestMapping("/login")
    public ModelAndView login(String username, String password, String type, HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
      User user = userService.login(username,password);
        if(user==null || ! type.equals(user.getType())){
            mv.setViewName("fail");
        }
      HttpSession session =request.getSession();
        session.setAttribute("username",username);
        session.setAttribute("password",password);


        if(type.equals("student")){

            mv.setViewName("studentlogin");
        }
      if(type.equals("teacher")){
          mv.setViewName("teacherlogin");
      }
      if(type.equals("gly")){
          mv.setViewName("glylogin");
      }
      System.out.println(mv.getViewName());
        return mv;
  }
    @RequestMapping("/AddUser")
    public ModelAndView adduser(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("AddUser");
        return mv;
    }
    @RequestMapping("/zhuce")
    public ModelAndView zhuce(User user){
        ModelAndView mv=new ModelAndView();
        userService.addUser(user);
        mv.setViewName("success");
        return mv;
    }
    @RequestMapping("/message")
    public ModelAndView message(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
       String username=(String) session.getAttribute("username");
        String password=(String) session.getAttribute("password");
        List<User> list=userService.findMyuser(username,password);
        mv.addObject("list",list);
        mv.setViewName("message");
        return mv;
    }


    @RequestMapping("/teachermessage")
    public String selectAll(Model model,HttpServletRequest request){
        HttpSession session=request.getSession();
        String username=(String) session.getAttribute("username");
        String password=(String) session.getAttribute("password");
        List<User> list=userService.findMyuser(username,password);
        model.addAttribute("list",list);
        return "teachermessage";
}
    @RequestMapping("/messagest")
    public String messagest(Model model){
        List<User> list =userService.selectAll();
        System.out.println(list);
        model.addAttribute("list",list);
        return "messagest";

    }


    @RequestMapping("/studentWork")
    public String work(Model model){
        List<Work> list =userService.selectStudentWork();
        model.addAttribute("list",list);
        return "studentWork";
    }
    @RequestMapping("/teacherWork")
    public String teacherwork(Model model){
        List<Work> list =userService.selectStudentWork();
        model.addAttribute("list",list);
        return "teacherWork";
    }
    @RequestMapping("/dotwork")
    public String dotwork(Model model, TeacherWork work){
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        TeacherWork work1=work;
        work1.setTime(date);
        userService.addTeacher(work1);
        return "teacherlogin";
    }


    @RequestMapping("/selectteacher")
    public ModelAndView selectteacher(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        String username= (String) session.getAttribute("username");
        String password= (String) session.getAttribute("password");
          User user=userService.selectTeacher1(username,password);
          System.out.println(user.getClasses());
          String cname=user.getClasses();
          String teacher=user.getTeacher();
          List<Work> list=userService.selectTeacher3(cname,teacher);
        System.out.println(list);
          mv.addObject("list",list);
        mv.setViewName("studentWork");
        return mv;
    }

    @RequestMapping("/updatepassword")
    public ModelAndView updatepassword(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("updatepassword");
        return mv;
    }
    @RequestMapping("/updatepwd")
    public ModelAndView updatepwd(String password,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        String username= (String) session.getAttribute("username");
        userService.updatePwd(password,username);
        mv.setViewName("message");
        return mv;
    }
    @RequestMapping("/deleteuser")
    public ModelAndView deleteuser(int id){
        ModelAndView mv=new ModelAndView();
        userService.delete(id);
        mv.setViewName("messagest");
        return mv;
    }


    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        session.removeAttribute("password");
        return "index";

    }
}

